package com.example.its2021606134chh.BusDiyModule;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.its2021606134chh.R;

import java.util.List;

/**
 * @ClassName: PayExpandableListviewAdapter$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 22/8/2021$
 * @Version: 1.0
 */
public class PayExpandableListviewAdapter extends BaseExpandableListAdapter {
    private List<BusPayData> groups;
    private String[][] childs;
    private Context context;
    private int isPay;

    public PayExpandableListviewAdapter(Context context,List<BusPayData> groups,String[][] childs, int isPay){
        this.context=context;
        this.groups=groups;
        this.childs=childs;
        this.isPay = isPay;
    }
    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return  childs[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_bus_pay_list,parent,false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tv_Start_End_Place = convertView.findViewById(R.id.tv_Start_End_Place);
            groupViewHolder.tv_Price = convertView.findViewById(R.id.tv_Price);
            groupViewHolder.tv_PayNum = convertView.findViewById(R.id.tv_PayNum);
            groupViewHolder.parent_Img_Right = convertView.findViewById(R.id.parent_Img_Right);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder)convertView.getTag();
        }
        groupViewHolder.tv_Start_End_Place.setText(groups.get(groupPosition).getStart_place() + "—" + groups.get(groupPosition).getEnd_place());
        groupViewHolder.tv_Price.setText("票价：￥" + groups.get(groupPosition).getPrice() + ".0");
        if (isPay == 0) {
            groupViewHolder.tv_PayNum.setText("订单编号：O" + (groupPosition + 1));
        } else {
            groupViewHolder.tv_PayNum.setText("订单编号：P" + (groupPosition + 1));
        }
        //如果是展开/收起状态，更改图标
        if (isExpanded){
            groupViewHolder.parent_Img_Right.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.fragmentation_ic_expandable));
        }else{
            groupViewHolder.parent_Img_Right.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.option_checked));
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView==null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_bus_diy_child,parent,false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.tvChildTitle = (TextView)convertView.findViewById(R.id.tvChildTitle);
            childViewHolder.tvChildText = (TextView)convertView.findViewById(R.id.tvChildText);
            convertView.setTag(childViewHolder);
        }else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        if (childPosition == 0) {
            childViewHolder.tvChildTitle.setText("乘车日期：");
        } else {
            childViewHolder.tvChildTitle.setText("                    ");
        }
        childViewHolder.tvChildText.setText(childs[groupPosition][childPosition]);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class GroupViewHolder {
        TextView tv_Start_End_Place, tv_Price, tv_PayNum;
        ImageView parent_Img_Right;
    }

    static class ChildViewHolder {
        TextView tvChildTitle, tvChildText;

    }
}
